package net.amsoft.iservice.management.webapp.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.amsoft.iservice.management.util.IBrokerConstants;
import net.amsoft.iservice.management.util.IBrokerUtil;
import net.amsoft.iservice.management.webapp.dataObject.LoginData;
import net.amsoft.iservice.util.IServiceConstants;
import net.amsoft.iservice.util.IServiceUtil;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 * Creates and initializes all the required session attributes <br>
 * 
 * a) ServerName: Name of the I-broker <br>
 * 
 * b) ServerPath: Absolute path of the application folder on server <br>
 * 
 * c) Service: Name of the service being customized <br>
 * 
 * d) Links: List of static links displayed in the left panel <br>
 * 
 * e) Banner: URL of ad-banner displayed in the top panel <br>
 * 
 * @author mukta
 * 
 */
public final class AdminLoginAction extends Action {

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception 
	{
        IServiceUtil.log(IServiceConstants.INFO, this, "execute(): entry");

        // Get Server Name
		LoginData oLoginData = (LoginData) request.getSession().getAttribute(IBrokerConstants.SESSION_LOGIN_OBJ);		
		if (oLoginData==null)
		{
	        IServiceUtil.log(IServiceConstants.INFO, this, "execute(): LoginData=null");
            IServiceUtil.log(IServiceConstants.DEBUG, this, 
                    "execute(): redirect to="+ mapping.getInputForward());
            return mapping.getInputForward();
		}
		
        String brokerName = oLoginData.getIBroker();
        request.getSession().setAttribute("ServerName", brokerName);
        IServiceUtil.log(IServiceConstants.INFO, this, "execute(): ServerName=" + brokerName);
        
		// Set ServerPath (reqd here for independent running of application)
		String serverPath = System.getProperty("catalina.home");
		if (serverPath.indexOf('\\') >= 0)
		    serverPath = serverPath.replace('\\', '/');
		serverPath = serverPath + "/webapps/" + IBrokerConstants.getProperty("ibroker.applicationName") + "/";
		request.getSession().setAttribute("ServerPath", serverPath);
        IServiceUtil.log(IServiceConstants.INFO, this, "execute(): ServerPath=" + serverPath);

		// Set Service Name
		request.getSession().setAttribute("Service", oLoginData.getService());
        IServiceUtil.log(IServiceConstants.INFO, this, "execute(): Service=" + oLoginData.getService());
		
		// Set links data
        String oNewString = IBrokerUtil.getLinks(brokerName, request.getSession().getAttribute("Service").toString());
		request.getSession().setAttribute("Links", oNewString);
        IServiceUtil.log(IServiceConstants.INFO, this, "execute(): Links=" + oNewString);
		
		// Set Banner
        String banner = IBrokerUtil.getAdBanner(brokerName, request.getSession().getAttribute("Service").toString());
		request.getSession().setAttribute("Banner", banner);
        IServiceUtil.log(IServiceConstants.INFO, this, "execute(): Banner=" + banner);

        IServiceUtil.log(IServiceConstants.INFO, this, "execute(): redirect to=" + 
        		IBrokerConstants.FWD_SUCCESS);
		return mapping.findForward(IBrokerConstants.FWD_SUCCESS);
	}
}
